package com.tutu.hosp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.tutu.hosp.repository.DepartmentRepository;
import com.tutu.hosp.service.DepartmentService;
import com.tutu.model.hosp.Department;
import com.tutu.vo.hosp.DepartmentQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @Program: yygh_parent
 * @Description:
 * @Author: JK.TuTu
 * @Create: 2022-09-24 18:43
 **/
@Slf4j
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 分页查询
     *
     * @param page              当前页码
     * @param limit             每页记录数
     * @param departmentQueryVo 查询条件
     */
    @Override
    public Page<Department> selectPage(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        //创建Pageable对象 设置当前页和每页记录数   0是第一页
        Pageable pageable = PageRequest.of(page - 1, limit);
        //创建examPle对象
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        Example<Department> example = Example.of(department, matcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    /**
     * 上传科室信息
     *
     * @param paramMap
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        log.info("传递的科室信息:{}", JSONObject.toJSONString(paramMap));
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
        //判断上传的科室是否存在
        Department departmentExist = departmentRepository.getDedepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (ObjectUtil.isNull(departmentExist)) {
            //插入
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            //更新
            departmentExist.setUpdateTime(new Date());
            departmentExist.setIsDeleted(0);
            departmentRepository.save(departmentExist);
        }
    }

    /**
     * 删除医院科室
     *
     * @param hoscode
     * @param depcode
     */
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDedepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (ObjectUtil.isNotNull(department)) {
            departmentRepository.deleteById(department.getId());
        }
    }
}
